export const formatTime = (date) => {
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`;
};
const formatNumber = (n) => {
	n = n.toString();
	return n[1] ? n : `0${n}`;
};
export const isInRegion = (point, region) => {
	let flag = false; //偶数次在外面，奇数次在里面
	let px = point.latitude;
	let py = point.longitude;
	for (let i = 0; i < region.length; i++) {
		let j = i + 1;
		//实际上就是没一条边都得算一下，首尾也有一条边得算在内
		if (i == region.length - 1) {
			j = 0;
		}
		//取相邻的两个点
		let sx = region[i].latitude;
		let sy = region[i].longitude;
		let tx = region[j].latitude;
		let ty = region[j].longitude; //判断一下点是不是和多边形的顶点重合
		if ((sx == px && sy == py) || (tx == px && ty == py)) {
			return 'on';
		}
		//不在顶点的话，判断一下是不是就在当前线上
		//做一条包含P点的，平行于y轴的直线
		//首先保证线段端点在射线两侧，和P点不是平行
		if ((sy < py && ty >= py) || (sy >= py && ty < py)) {
			let diff = (px - sx) * (ty - sy) - (py - sy) * (tx - sx);
			//判断一下px点是不是在边界上
			if (diff == 0) {
				return 'on';
			} else {
				//求一下交点
				let x = sx + ((py - sy) * (tx - sx)) / (ty - sy);
				//射线只往右边走
				//判断一下交点是否在当前点的右侧
				if (px > x) {
					flag = !flag;
				}
			}
		}
	}
	return flag;
};
export const isInCircle = (position, circle, radius) => {
	let lat1 = position.latitude;
	let lat2 = circle.latitude;
	let lng1 = position.longitude;
	let lng2 = circle.longitude;
	var radLat1 = (lat1 * Math.PI) / 180;
	var radLat2 = (lat2 * Math.PI) / 180;
	var a = radLat1 - radLat2;
	var b = (lng1 * Math.PI) / 180 - (lng2 * Math.PI) / 180;
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(
		Math.sin(b / 2), 2)));
	s = s * 6378.137;
	s = Math.round(s * 10000) / 10000;
	return s * 1000 < radius;
};
// module.exports = {
//     formatTime,
//     isInRegion,
//     isInCircle
// };